package com.sceneworker.extensions.capsule;

import com.acarter.propertytable.Property;
import com.acarter.propertytable.PropertySection;
import com.acarter.propertytable.propertyobject.FloatPropertyObject;
import com.acarter.propertytable.propertyobject.IntegerPropertyObject;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Capsule;
import com.jme2utils.staticutils.GenericJMEUtils;
import com.sceneworker.extensions.trimesh.TriMeshPropertyPage;

public class CapsulePropertyPage extends TriMeshPropertyPage {

	public CapsulePropertyPage() {
		super();
		
		PropertySection		lcl_section = new PropertySection("Capsule");
		lcl_section.addProperty(new Property("Axis Samples", new IntegerPropertyObject()));
		lcl_section.addProperty(new Property("Radial Samples", new IntegerPropertyObject()));
		lcl_section.addProperty(new Property("Sphere Samples", new IntegerPropertyObject()));
		lcl_section.addProperty(new Property("Radius", new FloatPropertyObject()));
		lcl_section.addProperty(new Property("Height", new FloatPropertyObject()));
		model.addPropertySection(0, lcl_section);
	}
	
	@Override
	protected void updateListeners(final Spatial spatial) {

		super.updateListeners(spatial);
		
		if(!(spatial instanceof Capsule)) { return; }
		
		final	Capsule		lcl_cap = (Capsule)spatial;
		
		PropertySection		lcl_section = model.getPropertySection("Capsule");
		
		IntegerPropertyObject		lcl_axisSamples = (IntegerPropertyObject)lcl_section.getProperty("Axis Samples").getPropertyObject();
		lcl_axisSamples.SetListener(new IntegerPropertyObject.I_IntegerPropertyObjectListener() {

			public int readValue() {
				return lcl_cap.getAxisSamples();
			}

			public void saveValue(int value) {
				GenericJMEUtils.updateGeometry(lcl_cap, value, lcl_cap.getRadialSamples(), lcl_cap.getSphereSamples(), lcl_cap.getRadius(), lcl_cap.getHeight());
			}

		});
		
		IntegerPropertyObject		lcl_radSamples = (IntegerPropertyObject)lcl_section.getProperty("Radial Samples").getPropertyObject();
		lcl_radSamples.SetListener(new IntegerPropertyObject.I_IntegerPropertyObjectListener() {

			public int readValue() {
				return lcl_cap.getRadialSamples();
			}

			public void saveValue(int value) {
				GenericJMEUtils.updateGeometry(lcl_cap, lcl_cap.getAxisSamples(), value, lcl_cap.getSphereSamples(), lcl_cap.getRadius(), lcl_cap.getHeight()); 
				lcl_cap.updateModelBound();		
				lcl_cap.updateGeometricState(0, true);
			}

		});
		
		IntegerPropertyObject		lcl_sphereSamples = (IntegerPropertyObject)lcl_section.getProperty("Sphere Samples").getPropertyObject();
		lcl_sphereSamples.SetListener(new IntegerPropertyObject.I_IntegerPropertyObjectListener() {

			public int readValue() {
				return lcl_cap.getSphereSamples();
			}

			public void saveValue(int value) {
				GenericJMEUtils.updateGeometry(lcl_cap, lcl_cap.getAxisSamples(), lcl_cap.getRadialSamples(), value, lcl_cap.getRadius(), lcl_cap.getHeight()); 
				lcl_cap.updateModelBound();		
				lcl_cap.updateGeometricState(0, true);
			}

		});
		
		FloatPropertyObject		lcl_radius = (FloatPropertyObject)lcl_section.getProperty("Radius").getPropertyObject();
		lcl_radius.SetListener(new FloatPropertyObject.I_FloatPropertyObjectListener() {

			public float readValue() {
				return lcl_cap.getRadius();
			}

			public void saveValue(float value) {
				GenericJMEUtils.updateGeometry(lcl_cap, lcl_cap.getAxisSamples(), lcl_cap.getRadialSamples(), lcl_cap.getSphereSamples(), value, lcl_cap.getHeight()); 
				lcl_cap.updateModelBound();		
				lcl_cap.updateGeometricState(0, true);
			}

		});
		
		FloatPropertyObject		lcl_height = (FloatPropertyObject)lcl_section.getProperty("Height").getPropertyObject();
		lcl_height.SetListener(new FloatPropertyObject.I_FloatPropertyObjectListener() {

			public float readValue() {
				return lcl_cap.getHeight();
			}

			public void saveValue(float value) {
				GenericJMEUtils.updateGeometry(lcl_cap, lcl_cap.getAxisSamples(), lcl_cap.getRadialSamples(), lcl_cap.getSphereSamples(), lcl_cap.getRadius(), value); 
				lcl_cap.updateModelBound();		
				lcl_cap.updateGeometricState(0, true);
			}

		});
	}

}
